require(GGally)

#Plots and correlation
gd<-(Data[,c(4,5, 7,8)])
gd<-gd[-which(is.na(gd$age== TRUE)),]
gd<-gd[-which(is.na(gd$sum_co2== TRUE)),]
names(gd)[1]<- "Capacity (MW)"
names(gd)[2]<- "Population-weighted damage"
names(gd)[3]<- "annual CO2 emission"
names(gd)[4]<- "Age"


lowerFn <- function(data, mapping, method = "lm", ...) {
  p <- ggplot(data = data, mapping = mapping) +
    geom_point(colour = "blue") +
    geom_smooth(method = method, color = "red", ...)
  p
}


ggpairs(gd, axisLabels = "internal", lower = list(continuous = wrap(lowerFn, method = "lm")),
        upper = list(continuous = wrap("cor", size = 6)))

##plot indices correlation
sa<-(sens_anlys[,c(10:20)])
sa<-sa[-which(is.na(sa$index== TRUE)),]
names(sa)[1]<- "PWD (100%)"
names(sa)[2]<- "Age (100%)"
names(sa)[3]<- "CO2 (100%)"
names(sa)[4]<- "Main index"
names(sa)[5]<- "PWD (50%)"
names(sa)[6]<- "Age (0%)"
names(sa)[7]<- "CO2 (50%)"
names(sa)[8]<- "Age (50%)"
names(sa)[9]<- "PWD (75%)"
names(sa)[10]<- "CO2 (75%)"
names(sa)[11]<- "Age (75%)"

ggpairs(sa, axisLabels = "internal", lower = list(continuous = wrap(lowerFn, method = "lm")),  upper = list(continuous = wrap("cor", method = "spearman",size = 4)))
#get correlation matrices
sa_pr_corr<-cor(sa, method="pearson", use = "complete.obs")
sa_sp_corr<-cor(sa, method="spearman", use = "complete.obs")
print(xtable(sa_pr_corr, type = "latex"), file = "sa_pr_corr.tex")
print(xtable(sa_sp_corr, type = "latex"), file = "sa_sp_cor.tex")

#------------------------------------------------------------------------
#Maps included in the manuscript are plotted using QGIS
##Steps:
#1. Read in the countries shapefile downloaded from https://www.diva-gis.org/Data
#2. Read in the CSV file as delimited layer
#3. Go to the data's properties (right click on the layer)
#4. Choose Symbology --> and choose graduated
#5. Choose the variable of interest to be plotted (for example: sum_capacity)
#6. Adjust Mode (down on th left) to quantiles
#7. Click Apply

#For variables that are string/text that need to be converted:
##Steps:
#1. Go to Processing toolbox--> Vector table-->refactor fields
#2. Choose the variable(s) of interest to be converted and click Run

#For top 20 plants to be mapped:
#1. Use top_20_plot.csv saved from the analysis and SA, which has the index values for the top 20 plants only 
#2. Map the variables accordingly